<?php



function simple_curl($url, $post = null, $header = [])
{
    // option
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);


    // curl_setopt($ch, CURLOPT_PROXY,'127.0.0.1:8888');//设置代理服务器


    $cookie_file = dirname(__FILE__).'/temp/cookie';
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    

    // post
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post);
    }

    // hedaer
    $headers = [
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) '
            .'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
        'Accept: */*',
        'Referer: http://callback.58.com/firewall/valid/988836761.do?'
            .'namespace=chuzulistphp&url=nj.58.com%2Fchuzu%2F0%2F',
        'Accept-Encoding: gzip, deflate',
        'Accept-Language: zh,zh-TW;q=0.9,en;q=0.8',
        'X-Requested-With: XMLHttpRequest',

    ];
    $headers = array_merge($headers, $header);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

    // exec
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $content  = curl_exec($ch);
    $header   = curl_getinfo($ch);
    curl_close($ch);
    return array(
        'httpcode' => $httpcode,
        'content'  => $content,
        'header'   => $header
    );
}



function download_file(string $url, $path)
{
    $ch = curl_init();
    $fd = fopen($path, 'w');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FILE, $fd);
    $resp = curl_exec($ch);
    fclose($fd);
    curl_close($ch);

    return $resp;
}